import { version } from '../../package.json'
import { Router } from 'express'
import { Sequelize, Op, QueryTypes } from 'sequelize'
import sequelize from '../models/sequelize'
import toRes from '../lib/toRes'
import ShangjiaModel from '../models/ShangjiaModel'
import md5 from 'md5-node'
import util from '../lib/util'
import jwt from 'jsonwebtoken'
import moment from 'moment'
// 商家
export default ({ config, db }) => {
	let api = Router()


// 用户登录接口
	api.all('/login', async (req, res) => {

		try {

			let userinfo = await ShangjiaModel.findOne({ where: { shangjiamingcheng: req.query.username, mima: req.query.password } })
			if (userinfo === null) {
				toRes.session(res, -1, '用户名或密码错误！')
				return
			}


			const token = jwt.sign(
				{
					id: userinfo.dataValues.id,
					username: userinfo.dataValues.shangjiamingcheng,
					role: '商家',
                    loginUserColumn: 'shangjiamingcheng',
                    tableName: 'shangjia'
				},
				config.jwtSecret,
				{
					expiresIn: 60 * 60 * 24 * 1
				}
			)

			userinfo.dataValues.token = token
			userinfo.dataValues.role = '商家'
			userinfo.dataValues.loginUserColumn = 'shangjiamingcheng'
			userinfo.dataValues.tableName = 'shangjia'
			delete userinfo.dataValues.mima
			req.session.userinfo = userinfo

			toRes.session(res, 0, '登录成功！', token)
		} catch(err) {

			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})

	// 用户退出接口
	api.all('/logout', (req, res) => {
		
		req.session.destroy(err => {
			toRes.session(res, 0, '退出成功！')
		})
	})

	// 注册接口
	api.post('/register', async (req, res) => {

		try {

			const userinfo = await ShangjiaModel.create(req.body)

			if (userinfo === null) {

				toRes.session(res, -1, '注册失败！')
			} else {

				toRes.session(res, 0, '注册成功！')
			}
		} catch(err) {
			
			toRes.session(res, -2, '请检查是否正确输入或用户名重复！', '', 200)
		}
	})

	// 获取session接口
	api.all('/session', async (req, res) => {

		try {

			toRes.record(res, 0, await ShangjiaModel.findOne({ where: { id: req.session.userinfo === undefined ? jwt.decode(req.headers.token).id : req.session.userinfo.id } }))
		} catch(err) {
			
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})

	// 忘记密码（找回密码）
	api.all('/resetPass', async (req, res) => {

		try {

			await ShangjiaModel.update({
				mima: '123456'
			}, {
				where: {
				  shangjiamingcheng: req.query.username
				}
			})

			toRes.session(res, 0, '密码已重置为：123456')
		} catch(err) {
			
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})


// 商家分页接口（后端）
	api.get('/page', async (req, res) => {
		try {
			let page = parseInt(req.query.page) || 1
			let limit = parseInt(req.query.limit) || 10
			let sort = req.query.sort || 'id'
			let order = req.query.order || 'asc'
			let where = {}
//构造商家后端查询的数据

var shangjiamingcheng = req.query.shangjiamingcheng
			if (shangjiamingcheng) {

				if (shangjiamingcheng.indexOf('%') != -1) {
					where.shangjiamingcheng = {
						[Op.like]: shangjiamingcheng
					}
				} else {
					where.shangjiamingcheng = {
						[Op.eq]: shangjiamingcheng
					}
				}
			}

var fuzeren = req.query.fuzeren
			if (fuzeren) {

				if (fuzeren.indexOf('%') != -1) {
					where.fuzeren = {
						[Op.like]: fuzeren
					}
				} else {
					where.fuzeren = {
						[Op.eq]: fuzeren
					}
				}
			}

var yingyexuke = req.query.yingyexuke
			if (yingyexuke) {

				if (yingyexuke.indexOf('%') != -1) {
					where.yingyexuke = {
						[Op.like]: yingyexuke
					}
				} else {
					where.yingyexuke = {
						[Op.eq]: yingyexuke
					}
				}
			}

var lianxidianhua = req.query.lianxidianhua
			if (lianxidianhua) {

				if (lianxidianhua.indexOf('%') != -1) {
					where.lianxidianhua = {
						[Op.like]: lianxidianhua
					}
				} else {
					where.lianxidianhua = {
						[Op.eq]: lianxidianhua
					}
				}
			}

var dizhi = req.query.dizhi
			if (dizhi) {

				if (dizhi.indexOf('%') != -1) {
					where.dizhi = {
						[Op.like]: dizhi
					}
				} else {
					where.dizhi = {
						[Op.eq]: dizhi
					}
				}
			}


let tableName = req.session.userinfo === undefined ? jwt.decode(req.headers.token).tableName : req.session.userinfo.tableName
if(tableName == 'shangjia') {
				where.shangjiamingcheng = {
					[Op.eq]: req.session.userinfo === undefined ? jwt.decode(req.headers.token).username : req.session.userinfo.shangjiamingcheng
				}
				if (where['userid'] != undefined) {
					delete where.userid
				}

			}


let result = await ShangjiaModel.findAndCountAll({
				order: [[sort, order]],
				where,
				offset: (page - 1) * limit,
				limit
			})
			result.currPage = page
			result.pageSize = limit
			toRes.page(res, 0, result)
		} catch(err) {
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})



 // 商家分页接口（前端）
	api.get('/list', async (req, res) => {
		try {
			let page = parseInt(req.query.page) || 1
			let limit = parseInt(req.query.limit) || 10
			let sort = req.query.sort || 'id'
			let order = req.query.order || 'asc'
			let where = {}

			//构造商家前端查询的数据
			
var shangjiamingcheng = req.query.shangjiamingcheng
			if (shangjiamingcheng) {

				if (shangjiamingcheng.indexOf('%') != -1) {
					where.shangjiamingcheng = {
						[Op.like]: shangjiamingcheng
					}
				} else {
					where.shangjiamingcheng = {
						[Op.eq]: shangjiamingcheng
					}
				}
			}

var fuzeren = req.query.fuzeren
			if (fuzeren) {

				if (fuzeren.indexOf('%') != -1) {
					where.fuzeren = {
						[Op.like]: fuzeren
					}
				} else {
					where.fuzeren = {
						[Op.eq]: fuzeren
					}
				}
			}

var yingyexuke = req.query.yingyexuke
			if (yingyexuke) {

				if (yingyexuke.indexOf('%') != -1) {
					where.yingyexuke = {
						[Op.like]: yingyexuke
					}
				} else {
					where.yingyexuke = {
						[Op.eq]: yingyexuke
					}
				}
			}

var lianxidianhua = req.query.lianxidianhua
			if (lianxidianhua) {

				if (lianxidianhua.indexOf('%') != -1) {
					where.lianxidianhua = {
						[Op.like]: lianxidianhua
					}
				} else {
					where.lianxidianhua = {
						[Op.eq]: lianxidianhua
					}
				}
			}

var dizhi = req.query.dizhi
			if (dizhi) {

				if (dizhi.indexOf('%') != -1) {
					where.dizhi = {
						[Op.like]: dizhi
					}
				} else {
					where.dizhi = {
						[Op.eq]: dizhi
					}
				}
			}

			let result = await ShangjiaModel.findAndCountAll({
				order: [[sort, order]],
				where,
				offset: (page - 1) * limit,
				limit
			})
			result.currPage = page
			result.pageSize = limit
			toRes.page(res, 0, result)
		} catch(err) {
			
			toRes.session(res, 401, '您的权限不够！', '', 200)
		}
	})



// 保存接口（后端）
	api.post('/save', async (req, res) => {

		try {
			Object.keys(req.body).forEach(item=>{
				if(req.body[item] == '')  delete req.body[item]
			})
			

			const userinfo = await ShangjiaModel.create(req.body)
			if (userinfo === null) {
				toRes.session(res, -1, '添加失败！')
			} else {
				toRes.session(res, 0, '添加成功！')
			}
		} catch(err) {
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})
    // 保存接口（前端）
	api.post('/add', async (req, res) => {
		try {
			Object.keys(req.body).forEach(item=>{
				if(req.body[item] == '')  delete req.body[item]
			})
			
			const userinfo = await ShangjiaModel.create(req.body)
			if (userinfo === null) {
				toRes.session(res, -1, '添加失败！')
			} else {
				toRes.session(res, 0, '添加成功！')
			}
		} catch(err) {
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})

	// 更新接口
	api.post('/update', async (req, res) => {

		try {
			await ShangjiaModel.update(req.body, {
				where: {
				  id: req.body.id
				}
			})
			toRes.session(res, 0, '编辑成功！')
		} catch(err) {
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})

	// 删除接口
	api.post('/delete', async (req, res) => {
		try {
			await ShangjiaModel.destroy({
				where: {
				  id: {
					[Op.in]: req.body
				  }
				}
			})
			toRes.session(res, 0, '删除成功！')
		} catch(err) {
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})
	
	// 详情接口（后端）
	api.all('/info/:id', async (req, res) => {
		try {
		toRes.record(res, 0, await ShangjiaModel.findOne({ where: { id: req.params.id } }))
		} catch(err) {
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})
    // 详情接口（前端）
	api.all('/detail/:id', async (req, res) => {
		try {
			toRes.record(res, 0, await ShangjiaModel.findOne({ where: { id: req.params.id } }))
		} catch(err) {

			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})



	// 获取需要提醒的记录数接口
	api.get('/remind/:columnName/:type', async (req, res) => {
		try {
			let sql = 'SELECT 0 AS count'
				if (req.params.type == 1) {
				if (req.query.remindstart) sql = "SELECT COUNT(*) AS count FROM shangjia WHERE " + req.params.columnName + " >= '" + req.query.remindstart + "'"
				if (req.query.remindend) sql = "SELECT COUNT(*) AS count FROM shangjia WHERE " + req.params.columnName + " <= '" + req.query.remindend + "'"
				if (req.query.remindstart && req.query.remindend) {
					sql = "SELECT COUNT(*) AS count FROM shangjia WHERE " + req.params.columnName + " >= '" + req.query.remindstart + "' AND " + req.params.columnName + " <= '" + req.query.remindend + "'"
				}
			}
			if (req.params.type == 2) {
				if (req.query.remindstart) {
					let remindStart = util.getDateTimeFormat(0 - req.query.remindstart, "yyyy-MM-dd")
					sql = "SELECT COUNT(*) AS count FROM shangjia WHERE " + req.params.columnName + " >= '" + remindStart + "'"
				}
				if (req.query.remindend) {
					let remindEnd = util.getDateTimeFormat(req.query.remindend, "yyyy-MM-dd")
					sql = "SELECT COUNT(*) AS count FROM shangjia WHERE " + req.params.columnName + " <= '" + remindEnd + "'"
				}
				if (req.query.remindstart && req.query.remindend) {
					let remindStart = util.getDateTimeFormat(0 - req.query.remindstart, "yyyy-MM-dd")
					let remindEnd = util.getDateTimeFormat(req.query.remindend, "yyyy-MM-dd")
					sql = "SELECT COUNT(*) AS count FROM shangjia WHERE " + req.params.columnName + " >= '" + remindStart + "' AND " + req.params.columnName + " <= '" + remindEnd + "'"
				}
			}
			const results = await sequelize.query(sql, {
				plain: true,
				raw: true,
				type: QueryTypes.SELECT
			})
			toRes.count(res, 0, results.count)
		} catch(err) {
				toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})


	api.get('/count', async (req, res) => {

		try {
			let where = {}
			const count = await ShangjiaModel.count({where});
			toRes.record(res, 0, count)
		} catch(err) {
			
			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})
api.get('/group/:columnName', async (req, res) => {
		try {
			let sql = ""
			let columnName = req.params.columnName
			let tableName = "shangjia"
			let where = " WHERE 1 = 1 "
			
			sql = "SELECT COUNT(*) AS total, " + columnName + " FROM " + tableName + where + " GROUP BY " + columnName
			toRes.record(res, 0, await sequelize.query(sql, {
				plain: false,
				raw: true,
				type: QueryTypes.SELECT
			}))
		} catch(err) {

			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})
	api.get('/value/:xColumnName/:yColumnName', async (req, res) => {

		try {

			let sql = ""
			let xColumnName = req.params.xColumnName
			let yColumnName = req.params.yColumnName
			let tableName = "shangjia"
			let where = " WHERE 1 = 1 "
			
			if ("shangjia" == "orders") {
				where += " AND status IN ('已支付', '已发货', '已完成') ";
			}
			sql = "SELECT " + xColumnName + ", SUM(" + yColumnName + ") AS total FROM " + tableName + where + " GROUP BY " + xColumnName + " DESC"
			
			toRes.record(res, 0, await sequelize.query(sql, {
				plain: false,
				raw: true,
				type: QueryTypes.SELECT
			}))
		} catch(err) {

			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})

	api.get('/value/:xColumnName/:yColumnName/:timeStatType', async (req, res) => {

		try {
			
			let sql = ""
			let xColumnName = req.params.xColumnName
			let yColumnName = req.params.yColumnName
			let timeStatType = req.params.timeStatType
			let tableName = "shangjia"
			let where = " WHERE 1 = 1 "
			
			if ("shangjia" == "orders") {
				where += " AND status IN ('已支付', '已发货', '已完成') ";
			}

            if (config.dbConnection.dbtype.toLowerCase() == "mysql") {
                if (timeStatType == "日")
                    sql = "SELECT DATE_FORMAT(" + xColumnName + ", '%Y-%m-%d') " + xColumnName + ", sum(" + yColumnName + ") total FROM " + tableName + where + " GROUP BY DATE_FORMAT(" + xColumnName + ", '%Y-%m-%d')";
                if (timeStatType == "月")
                    sql = "SELECT DATE_FORMAT(" + xColumnName + ", '%Y-%m') " + xColumnName + ", sum(" + yColumnName + ") total FROM " + tableName + where + " GROUP BY DATE_FORMAT(" + xColumnName + ", '%Y-%m')";
                if (timeStatType == "年")
                    sql = "SELECT DATE_FORMAT(" + xColumnName + ", '%Y') " + xColumnName + ", sum(" + yColumnName + ") total FROM " + tableName + where + " GROUP BY DATE_FORMAT(" + xColumnName + ", '%Y')";
            } else {
                if (timeStatType == "日")
                    sql = "SELECT DATE_FORMAT(VARCHAR(10)," + xColumnName + ", 120) " + xColumnName + ", sum(" + yColumnName + ") total FROM " + tableName + where + " GROUP BY DATE_FORMAT(VARCHAR(10)," + xColumnName + ", 120)";
                if (timeStatType == "月")
                    sql = "SELECT DATE_FORMAT(VARCHAR(7)," + xColumnName + ", 120) " + xColumnName + ", sum(" + yColumnName + ") total FROM " + tableName + where + " GROUP BY DATE_FORMAT(VARCHAR(7)," + xColumnName + ", 120)";
                if (timeStatType == "年")
                    sql = "SELECT DATE_FORMAT(VARCHAR(4)," + xColumnName + ", 120) " + xColumnName + ", sum(" + yColumnName + ") total FROM " + tableName + where + " GROUP BY DATE_FORMAT(VARCHAR(4)," + xColumnName + ", 120)";
            }
			toRes.record(res, 0, await sequelize.query(sql, {
				plain: false,
				raw: true,
				type: QueryTypes.SELECT
			}))
		} catch(err) {

			toRes.session(res, 500, '服务器错误！', '', 500)
		}
	})
return api
}